home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / JOYSTICK.SWG / 0008_Joystick Routines.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  2KB  |  110 lines

  1. {
  2. > Can anybody give me any info on how to read signals from pins on say
  3. > COM2: or from LPT1: or even from The joystick port? I think it has
  4. > been done with the PORT command or something, but what are the values
  5. > to use to read them with?
  6.  
  7. As mentioned in the Neural_Net Echo:
  8. }
  9.  
  10. UNIT joys;
  11.  
  12. {Joystick interface for Turbo Pascal.}
  13. {Copyright 1993 by Wesley R. Elsberry.  Released for noncommercial use.}
  14. {NO WARRANTY.}
  15.  
  16. INTERFACE
  17.  
  18. VAR
  19.   xcor, ycor,
  20.   fire1, fire2 : WORD;
  21.  
  22. PROCEDURE joy;     { basic info for first joystick }
  23. PROCEDURE testjoy; { minimal test routine }
  24.  
  25. IMPLEMENTATION
  26.  
  27. { Significant aid was had from the example given in:
  28.   programm to read joystick : turbo c 2.0 - Jean-Yves Vinet }
  29.  
  30. CONST
  31.   JOYPORT = $201;
  32.   STROUT  = $FF;
  33.   STRCMP  = $FFFF;
  34.  
  35. VAR
  36.   inread,
  37.   temp   : BYTE;
  38.  
  39. PROCEDURE joy;
  40. VAR
  41.   done : BOOLEAN;
  42. BEGIN
  43.   done := FALSE;
  44.   xcor := 0;
  45.   ycor := 0;
  46.   port[JOYPORT] := STROUT;
  47.  
  48.   while (NOT done) DO
  49.   BEGIN
  50.     if ((port[JOYPORT] AND 1) = 0) then
  51.       done := TRUE;
  52.     INC(xcor);
  53.     if (xcor = STRCMP) then
  54.       done := true;
  55.   END;
  56.  
  57.   while ((port[JOYPORT] AND 2) <> 0) DO ;
  58.  
  59.   done := FALSE;
  60.   port[JOYPORT] := STROUT;
  61.  
  62.   while (NOT done) DO
  63.   BEGIN
  64.     if ((port[JOYPORT] AND 2) = 0) then
  65.       done := TRUE;
  66.     INC(ycor);
  67.     if (ycor = STRCMP) then
  68.       done := TRUE;
  69.   END;
  70.  
  71.   inread := port[JOYPORT];
  72.  
  73.   { Button A at $10, B at $20, C at $40, D at $80. }
  74.   if ((inread AND $10) <> $10) then
  75.     fire1 := 1;
  76.   if ((inread AND $20) <> $20) then
  77.     fire2 := 1;
  78. END;
  79.  
  80. {If you want to grab the second joystick values, the X coordinate
  81. should be gotten from comparing port[JOYPORT] to 4, the Y coordinate
  82. from comparing port[JOYPORT] to 8.}
  83.  
  84. PROCEDURE testjoy;
  85. BEGIN
  86.   while TRUE do
  87.   BEGIN
  88.     joy;
  89.     WRITELN(xcor : 5, '  ', ycor, '  ', fire1, '  ', fire2);
  90.     xcor  := 0;
  91.     ycor  := 0;
  92.     fire1 := 0;
  93.     fire2 := 0;
  94.   END;
  95. END;
  96.  
  97. BEGIN
  98.   {No initialization required.}
  99. END.
  100.  
  101. {
  102. The above was a pretty quick and dirty approach to grabbing values
  103. off the game card.  I'm sure that there are better means of doing it,
  104. but I haven't put in the time to find them.  Interestingly enough,
  105. the Turbo C version mentioned in the comments of the unit above does
  106. not give as large a value for the maximum displacement of a joystick,
  107. which is an indicator that the Turbo Pascal code is faster than its
  108. equivalent Turbo C counterpart.
  109. }
  110.